package com.yourbatis.syntax;

public class IfStatement implements Statement {
    private final Expression condition;
    private final Statement thenBranch;
    private final Statement elseBranch;


    public IfStatement(Expression condition, Statement thenBranch, Statement elseBranch) {
        this.condition = condition;
        this.thenBranch = thenBranch;
        this.elseBranch = elseBranch;
    }



    @Override
    public Object execute() {
        Object val = null;
        Boolean result = (Boolean) condition.eval();
        if (result) {
            val = thenBranch.execute();
        } else if (elseBranch != null) {
            val = elseBranch.execute();
        }
        return val;
    }
}
